<template>
	<view>
		<u-navbar :border-bottom="false"></u-navbar>
		<view class="content">
			<view class="xxxx_title">
				<image class="logo" src="@/static/img/logo.png" mode="widthFix"></image>
				<h1>武信校园墙</h1>
				<p>大学生交友平台</p>
			</view>
			<view class="xxxx_btn">
				<view class="wx_login" @click="wxlogin">
					<image class="img wx_img" src="@/static/img/20.png" mode="widthFix"></image>
					微信登录
				</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
import { wxLogin } from '@/api/index.js';
export default {
	data() {
		return {
			code: ''
		};
	},
	onLoad() {
		this.getCode();
		const db = uniCloud.database();
		db.collection('socket_user')
			.get()
			.then(res => {
				console.log(res, '获取数据');
				// if (res.result.code === 0) {
				// 	this.user_list = res.result.data
				// }
			});
	},
	methods: {
		getCode() {
			//获取临时登陆code
			uni.login({
				provider: 'weixin',
				success: loginRes => {
					console.log(loginRes, '授权信息');
					this.code = loginRes.code;
				}
			});
		},
		wxlogin() {
			//微信登录
			wx.getUserProfile({
				desc: '用于获取用户资料的完整性', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				success: res => {
					console.log(res, '授权成功');
					wxLogin({
						code: this.code, //code验证码 五分钟有效期
						encryptedData: res.encryptedData,
						iv: res.iv
					})
						.then(res => {
							//openid  oKHUG5VAZ4NpYMGZyZm3TGZUctMY
							console.log(res, '登录结果');
							if (res.code === 200) {
								this.$refs.uToast.show({
									title: res.prompt,
									type: 'success'
								});
								//uni.$emit('update_token');
								uni.setStorageSync('userInfo', res.data.userInfo);
								uni.setStorageSync('token', res.data.token);
								setTimeout(() => {
									uni.switchTab({
										url: '/pages/index/index'
									});
									/* uni.navigateBack({
									    delta: 1
									}); */
								}, 800);
							} else {
								this.getCode();
								this.$refs.uToast.show({
									title: res.prompt,
									type: 'error'
								});
							}
						})
						.catch(err => {
							this.getCode();
							console.log(err);
						});
				},
				fail: err => {
					console.log('用户放弃了授权登录');
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.xxxx_title {
	text-align: center;
	display: flex;
	font-size: 0;
	flex-direction: column;

	.logo {
		width: 200rpx;
		height: auto;
		margin: 100rpx auto 20rpx;
	}

	h1 {
		font-size: 50rpx;
	}

	p {
		margin-top: 10rpx;
		color: #828282;
		font-size: 25rpx;
	}
}

.xxxx_btn {
	margin-top: 200rpx;

	> view {
		width: 550rpx;
		border-radius: 50rpx;
		border-style: solid;
		border-width: 1rpx;
		padding: 22rpx 0;
		text-align: center;
		margin: 0 auto 30rpx;
	}

	.img {
		width: 35rpx;
		height: auto;
		margin-right: 20rpx;

		&.wx_img {
		}
	}

	.wx_login {
		border-color: #18b11e;
		color: #18b11e;
	}
}
</style>
